<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Article.php
 * Date: 2019/2/14
 * Time: 15:33
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Admin;
use app\common\model\Article as ArticleModel;
use app\common\model\ArticleType as ArticleTypeModel;
use think\facade\Request;

class Article extends Admin
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $article = new ArticleModel();
        if(Request::isAjax())
        {
            return $article->tableData(input('param.'));
        }
        $articleTypeModel = new ArticleTypeModel();
        $list = $articleTypeModel->select();
        return $this->fetch('', ['list' => $list]);
    }

    /**
     * 文章添加
     * @return array|mixed
     */
    public function add()
    {
        if(Request::isPost())
        {
            $article = new ArticleModel();
            return $article->addData(input('param.'));
        }
        $articleTypeModel = new ArticleTypeModel();
        $list = $articleTypeModel->select();
        return $this->fetch('add',['list'=> $articleTypeModel->getTree($list)]);
    }

    /**
     * 文章编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $articleModel = new ArticleModel();
        if(Request::isPost())
        {
            return $articleModel->saveData(input('param.'));
        }
        $info = $articleModel->with('articleType')->where('id',input('param.id/d'))->find();
        if (!$info)
        {
            return error_code(10002);
        }
        return $this->fetch('edit',['info'=>$info]);
    }

    /**
     * 文章删除
     * @return array
     */
    public function del()
    {
        $article = new ArticleModel();
        $result = ['status' => true,'msg' => '删除成功','data'=>''];
        if (!$article->destroy(input('param.id/d'))) {
            $result['status'] = false;
            $result['msg'] = '删除失败';
        }
        return $result;
    }
}